﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using NetDon.OA.Interface.Admin;
using NetDon.Utilities;

namespace NetDon.OA.Interface.Admin
{

    [Serializable]
    public class DepartmentData : DomainObject, IHierarchyData
    {
        public Guid DepartmentId { get; set; }
        public string DepartmentCode { get; set; }
        public string DepartmentName { get; set; }
        public string Manager { get; set; }
        public string DepartmentDesc { get; set; }
        public string Address { get; set; }

        public string Telephone { get; set; }
        public string Fax { get; set; }
        public int? NodeLevel { get; set; }
        public Guid? ParentId { get; set; }

        public Guid? CompanyId { get; set; }

        #region Implementation of IHierarchyData

        public IHierarchicalEnumerable GetChildren()
        {
            var service = ServiceContainer.GetService<IDepartmentService>();
            return new DepartmentDataCollection(service.GetChildDepartments(DepartmentId));
        }

        public IHierarchyData GetParent()
        {
            if (ParentId.HasValue)
            {
                var service = ServiceContainer.GetService<IDepartmentService>();
                return service.GetDepartment(ParentId.Value);
            }
            return null;
        }

        public bool HasChildren
        {
            get
            {
                var service = ServiceContainer.GetService<IDepartmentService>();
                return service.GetChildDepartments(DepartmentId).Count > 0;
            }
        }

        public string Path
        {
            get;
            set;
        }

        public object Item
        {
            get { return this; }
        }

        public string Type
        {
            get { return typeof(DepartmentData).FullName; }
        }

        #endregion
    }
    public class DepartmentDataCollection : Collection<DepartmentData>, IHierarchicalEnumerable
    {
        public DepartmentDataCollection()
        {
        }

        public DepartmentDataCollection(IEnumerable<DepartmentData> categories)
        {
            foreach (var category in categories)
            {
                Add(category);
            }
        }
        #region Implementation of IHierarchicalEnumerable

        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            return (DepartmentData)enumeratedItem;
        }

        #endregion
    }
}


